﻿#include "mcheaddialog.h"
#include "ui_mcheaddialog.h"

McHeadDialog::McHeadDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::McHeadDialog)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明

    initWidget();
    initControl();
}

McHeadDialog::~McHeadDialog()
{
    delete ui;
}

void McHeadDialog::on_buttonOk_clicked()
{
    done(1);
}

void McHeadDialog::on_buttonCancel_clicked()
{
    done(0);
}

void McHeadDialog::updataUi(int btnIndex, MyButton *button)
{
    if(btnIndex > 8 || btnIndex < 0)
        return;
    bitset<8> charBit(m_mcHeadSw);
    m_mcHeadSw = setBit(m_mcHeadSw, btnIndex, !charBit[btnIndex]);

    bitset<8> charBitFan(m_mcHeadSw);
    cout << charBitFan << endl;

    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    if(charBitFan.test(btnIndex)) // 建议使用test
    {button->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");}
    else
    {button->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");}
}

void McHeadDialog::refreshUi()
{
    bitset<8> charBit(m_mcHeadSw);
    if (charBit[0] == 1)
    {
        ui->pushButton->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
    else
    {
        ui->pushButton->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }
    if(charBit[1] == 1)
    {
        ui->pushButton_2->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
    else
    {
        ui->pushButton_2->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }

    if(charBit[2] == 1)
    {
        ui->pushButton_3->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
     else
    {
        ui->pushButton_3->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }

    if(charBit[3] == 1)
    {
        ui->pushButton_4->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
    else
    {
        ui->pushButton_4->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }

    if(charBit[4] == 1)
    {
        ui->pushButton_5->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
    else
    {
        ui->pushButton_5->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }

    if(charBit[5] == 1)
    {
        ui->pushButton_6->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
    else
    {
        ui->pushButton_6->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }

    if(charBit[6] == 1)
    {
        ui->pushButton_7->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
    else
    {
        ui->pushButton_7->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }

    if(charBit[7] == 1)
    {
        ui->pushButton_8->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));border-radius:" + borderRadius() + "px;color:rgb(255,255,255);");
    }
    else
    {
        ui->pushButton_8->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(100, 100, 100), stop:1 rgb(100, 100, 100));border-radius:" + borderRadius() + "px;color:rgb(234, 234, 234);");
    }

}

char McHeadDialog::setBit(char data, int setoff, bool value)
{
    bitset<8> charBit(data);
    charBit.set(setoff, value);
    char res = (char)charBit.to_ulong();
    return res;
}

void McHeadDialog::setChar(char data)
{
    m_mcHeadSw = data;
    refreshUi();
}

unsigned char McHeadDialog::getChar()
{
    return m_mcHeadSw;
}

void McHeadDialog::on_pushButton_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 0), Q_ARG(MyButton *, button));
}

void McHeadDialog::on_pushButton_2_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 1), Q_ARG(MyButton *, button));
}

void McHeadDialog::on_pushButton_3_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 2), Q_ARG(MyButton *, button));
}

void McHeadDialog::on_pushButton_4_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 3), Q_ARG(MyButton *, button));
}

void McHeadDialog::on_pushButton_5_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 4), Q_ARG(MyButton *, button));
}

void McHeadDialog::on_pushButton_6_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 5), Q_ARG(MyButton *, button));
}

void McHeadDialog::on_pushButton_7_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 6), Q_ARG(MyButton *, button));
}

void McHeadDialog::on_pushButton_8_clicked()
{
    MyButton *button = (MyButton*) this->sender() ;
    QMetaObject::invokeMethod(this, "updataUi", Qt::DirectConnection, Q_ARG(int , 7), Q_ARG(MyButton *, button));
}

void McHeadDialog::initWidget()
{
    m_sheetBtnList.append(ui->pushButton);
    m_sheetBtnList.append(ui->pushButton_2);
    m_sheetBtnList.append(ui->pushButton_3);
    m_sheetBtnList.append(ui->pushButton_4);
    m_sheetBtnList.append(ui->pushButton_5);
//    m_sheetBtnList.append(ui->pushButton_6);
//    m_sheetBtnList.append(ui->pushButton_7);
//    m_sheetBtnList.append(ui->pushButton_8);
    ui->pushButton_6->setVisible(false);
    ui->pushButton_7->setVisible(false);
    ui->pushButton_8->setVisible(false);
    //    QList<QPushButton* > btns = this->findChildren<QPushButton *>();
}

void McHeadDialog::initControl()
{
    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为任意分辨率
void McHeadDialog::initResolution()
{
    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

    this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    this->move((GLB_SCR_WIGHT*factoryX-this->width())/2+g_mainWidgetPos.x(),(GLB_SCR_HEIGHT*factoryY-this->height())/2+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0*factoryX, 0*factoryY, GLB_SCR_WIGHT*factoryX, GLB_SCR_HEIGHT*factoryY);
    ui->framePageBtn->setGeometry(0*factoryX,(GLB_SCR_HEIGHT-GLB_LBUT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,GLB_SCR_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonOk->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*1+GLB_LBUT_WIGHT*2))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonCancel->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*0+GLB_LBUT_WIGHT*1))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);

    int bgX = (GLB_SCR_WIGHT - GLB_LBUT_WIGHT*m_sheetBtnList.size() - GLB_BAS_WIGHT * (m_sheetBtnList.size() -1))/2;
    for(int i = 0; i < m_sheetBtnList.size(); i++){
        m_sheetBtnList[i]->setGeometry((bgX+i*(GLB_LBUT_WIGHT + GLB_BAS_WIGHT))*factoryX, GLB_SCR_HEIGHT/2*factoryY, GLB_LBUT_WIGHT*factoryX, GLB_LBUT_HEIGHT*factoryY);
        m_sheetBtnList[i]->setText(QString("%1").arg(i+1));
        m_sheetBtnList[i]->setFont(fontSize_M());
        m_sheetBtnList[i]->setStyleSheet(titleTextColour());
    }
}

void McHeadDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    ui->frameBack->setStyleSheet(transparentStyle());
    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));

    ui->buttonOk->setStyleSheet(confirmIconStyle());
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()),12);

    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()),12);

}
